layui.config({
	base : "js/"
}).use(['form','element','layer','jquery'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		element = layui.element(),
		$ = layui.jquery;

	$(document).on('click', '.add_menu', function () {
		var level = 0, index = 0, two_index = 0;
		var _this = $(this);
		if (_this.hasClass('add_level_one')) {
			level = 1;
			index = $("tbody tr:last").data('index') + 1;
		} else {
			index = _this.parents('tr').data('index');
			var last_two_index = _this.parents('tr').next().data('two-index');
			if (last_two_index > 0) {
				two_index = last_two_index + 1;
			} else {
				two_index = 1;
			}
		}
		var html = '<tr data-index="'+ index +'" data-two-index="'+ two_index +'">' +
			'<td>';
        if(level == 1) {
            html += '<i class="layui-icon">&#xe600;</i></td>';
        }else {
            html += '<i class="layui-icon">&#xe623;</i></td>';
		}
        html += '<td>';
		if(level == 1) {
			html += '<input type="text" class="layui-input" name="name['+index+']['+two_index+']" value="" style=" width:230px; font-size: 8px; float: left; ">';
		} else {
			html += '<input type="text" class="layui-input" name="name['+index+']['+two_index+']"  value="" style=" width:230px; font-size: 8px; float: left; margin-left: 2em">';
		}
		html += '<p style="float: left; margin-left: 5px; margin-bottom: 0;">';
		if(level == 1) {
			html += '<a class="layui-btn add_menu"><i class="layui-icon">&#xe654;</i> 添加</a>';
		}
		html += '<a class="layui-btn  layui-btn-danger del_menu"><i class="layui-icon">&#xe640;</i> 删除</a>' +
			'</p>' +
			'</td>' +
			'<td>' +
			'<div class="form-group" style=" margin-bottom: 0">' +

			'<select class="form-control m-b" name="account['+index+']['+two_index+']" style="height: 23px; padding: 0 12px; font-size: 8px; margin: 0;">' +

			'<option selected="" value="view">链接</option>' +
			'<option value="click">触发关键字</option>' +
			'<option value="scancode_push">扫码</option>' +
			'<option value="scancode_waitmsg"> 扫码（等待信息）</option>' +
			'<option value="pic_sysphoto">系统拍照发图</option>' +
			'<option value="pic_photo_or_album">拍照或者相册发图</option>' +
			'<option value="pic_weixin">微信相册发图</option>' +
			'<option value="location_select">地理位置</option>' +
			'</select>' +
			'</div>' +
			'</td>' +
			'<td>' +

			'<input type="text" class="layui-input" name="value['+index+']['+two_index+']" value="" style=" font-size: 8px; width: 100%;">' +
			'</td>' +
			'</tr>';
		if(level == 1)
			$('tbody').append(html);
		else
			_this.parents('tr').after(html);
        form.render('select'); //刷新select选择框渲染
	});

	// 删除
	$(document).on('click', '.del_menu', function () {
		var _this = $(this);
		_this.parents('tr').remove();
	});

    // 保存
	$("#wx_menu_form").submit(function (e) {
		e.preventDefault();
		var _this = $(this);
		console.log(_this.serialize());
		$.ajax({
			type : _this.attr('method'),
			url : _this.attr('action'),
			data : _this.serialize(),
			dataType : 'json',
			success : function (msg) {
				if (msg.code == 200) {
					layer.msg(msg.msg, {icon: 6,time:1000});
				} else {
					layer.msg(msg.msg, {icon: 5,time:1000});
				}
			}
		});
	});

	// 同步微信菜单
	$(".sync-menu").click(function () {
		$.get("/admin/wechat/sync_menu", function (msg) {
			if (msg.code == 200) {
				layer.msg(msg.msg, {icon: 6,time:1000});
			} else {
				layer.msg(msg.msg, {icon: 5,time:1000});
			}
		});
	});
});
